/**
 * 生成随机字符串
 * @param {number} len 字符串长度
 * @return {string}
 */
 export function randomString(len:number) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    var maxPos = $chars.length;
    var pwd = '';
    for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

/**
 * 获取两个日期之间的日期
 * @param starDay
 * @param endDay
 * @returns
 */
export function getDayAll(starDay:Date, endDay:Date) {

　　 var arr = [];
	var dates = [];

	// 设置两个日期UTC时间
　　　var db = new Date(starDay);
　　　var de = new Date(endDay);

	// 获取两个日期GTM时间
	var s = db.getTime() - 24 * 60 * 60 * 1000;
	var d = de.getTime() - 24 * 60 * 60 * 1000;

	// 获取到两个日期之间的每一天的毫秒数
　　　for (var i = s; i <= d;) {
　　　　　　i = i + 24 * 60 * 60 * 1000;
		arr.push(i)
　　　}

	// 获取每一天的时间  MM-DD
	for( var j in arr ){
		var time = new Date(arr[j]);
		var year = time.getFullYear();
		var mouth = (time.getMonth() + 1)>=10?(time.getMonth() + 1):('0'+(time.getMonth() + 1));
		var day = time.getDate()>=10?time.getDate():('0'+time.getDate());
		// var YYMMDD = year + '-' + mouth + '-' + day;
		var MMDD = mouth + '-' + day;
		dates.push(MMDD)
	}

	return dates
}

export function dateFormat(d, fmt = 'yyyy-MM-dd hh:mm:ss') {
	if (typeof d == 'string') {
		if (d.length == 10) d = d + '000'
		if (!isNaN(d)) d = Number(d)
	}
	if (typeof d == 'number') {
		if (d.toString().length == 10) d = d * 1000
	}
	if (!(d instanceof Date)) {
		d = new Date(d)
	}
	var o = {
		"M+": d.getMonth() + 1, //月份
		"d+": d.getDate(), //日
		"h+": d.getHours(), //小时
		"m+": d.getMinutes(), //分
		"s+": d.getSeconds(), //秒
		"q+": Math.floor((d.getMonth() + 3) / 3), //季度
		"S": d.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

/**将对象转成数组 */
export function objectToArray(obj,key:string='id'){
	let arr=[]
	Object.keys(obj).forEach(k=>{
		obj[k][key]=k
		arr.push(obj[k])
	})
	return arr
}

/**从数组中随机返回一个成员 */
export function getRandomChildFromArray(arr) {
    const randomIndex = Math.floor(Math.random() * arr.length);
    return arr[randomIndex];
}

/**在最小值和最大值之间随机取一个数 */
export function getRandomNumberByRange(min: number, max: number): number {
	if(min==max){
		return min
	}
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

/**从数组对象中随机获取多个成员 */
export function getMultipleChildByWeight(arr:any[],weightKey:string){
	let res=[]
	for (let i = 0; i < arr.length; i++) {
		let item = arr[i];
		if(Math.random()<item[weightKey]){
			res.push(item)
		}
	}
	return res
}

/**根据对象中的某个ke作为权重 随机返回对象*/
export function getRandomChildFromKey(arr: any, weightKey: string = "weight") {
    let totalWeight: number = arr.reduce((acc, cur: any) => acc + Number(cur[weightKey]), 0) as number;
    let rand = Math.random() * totalWeight;
	for (let i = 0; i < arr.length; i++) {
		let element = arr[i];
		if (rand < element[weightKey]) {
            return element;
        }
        rand -= element[weightKey];
	}
}
